<?php
/*
	排队管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
    //验证是否为员工
$work = pdo_get('jy_dish_staff',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
if($work['id']){
	$group=pdo_get('jy_dish_staff_group',array('id'=>$work['group_id']));
    //string转数组	
	$ops=explode(";",$group['ops']);
	//是否有操作权限
	if(in_array('4',$ops)){
	}
	else{
		exi('无操作权限','error');
	}
}
else{
	exi('非员工无法访问！','error');
}
if($_RQ['store_id']){
	$store_id = intval($_RQ['store_id']);
	$this->session('store_id',$store_id);
}
if(!$store_id){
	$store_id=$this->session('store_id');
}
if(!$store_id){
	exi('请先选择门店！','error','jy_dish/index/');
}
if($_RQ['op']=='list'){
	//查询会员排队
	$queue = pdo_getall('jy_dish_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>'0'));
	foreach($queue as $a){
		$b[]=$a['queue_id'];	
	}
	   $b = array_unique($b);
	//返回当前所有启用且有人排队的队列
	$queuelist = pdo_getall('jy_dish_queue',array('store_id'=>$store_id,'uniacid'=>$_SESSION['uniacid'],'status'=>'0','id'=>$b));
	$return['queuelist'] = $queuelist;
	set_html('title','排号队列');
	exi($return);
}
if($_RQ['op']=='jiaohao'){
	//$queue_id=intval($_RQ['queue_id']);
	$queue_id=1;
	if(!$queue_id){
		exi('请先选择队列！','error');
	}
	$customer = pdo_getall('jy_dish_queue_member',array('store_id'=>$store_id,'queue_id'=>$queue_id,'uniacid'=>$_SESSION['uniacid'],'status'=>'0'));
	//exi($customer,'error');
	if($customer){
	paixu($customer,'createtime');
	//当前队列在排队中的第一个顾客id（按排队时间先后）
	$customer_id = $customer['0']['id'];
	//将该顾客的状态改为叫号
	pdo_update('jy_dish_queue_member',array('status'=>'1'),array('id'=>$customer_id));
	//获取门店名
	$title = pdo_getcolumn('jy_dish_store',array('id'=>$store_id),'title');
	//查询顾客的微信昵称
	$nickname=pdo_getcolumn('core_fans',array('mid'=>$customer['0']['mid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
	//通知该顾客前往用餐
	$postdata = array(
					'first' => array(
						'value' => '您好，请前往就餐！',
						'color' => '#990000'
					),
					'keyword1' => array(
						'value' => $title,
						'color' => '#ff510'
					),
					'keyword2' => array(
						'value' => $nickname,
						'color' => '#ff510'
					),
					'keyword3' => array(
						'value' => '',
						'color' => '#ff510'
					),
					'remark' => array(
						'value' => date("Y-m-d H:i:s",TIMESTAMP),
						'color' => '#ff510'
					),							
	);
	$tpl=$this->plugin_setting('message_tpl');
	$url=custom_url('jy_dish/queue_manage/',true);
	cfc('account')->sendTplNotice($customer['0']['mid'],$tpl['call'],$postdata,$url,'#980000');
}else{
		exi('当前队列无正在排队客户,请重新选择队列','error');
}
	//查询当前队列会员排队
$queue = pdo_getall('jy_dish_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'queue_id'=>$queue_id,'status'=>'0'));
$setting =$this->store_setting($store_id,'queue');
if($queue){
	foreach($queue as $a){
		$b[]=$a['mid'];	
	}
	$b = array_unique($b);
	//获得设置数量的顾客
	$b = array_slice($b,0,$setting['num']);
	foreach($b as $c){
		//该客户信息
		$customer = pdo_get('jy_dish_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'queue_id'=>$queue_id,'mid'=>$c));
		//在该客户之前的人数
		$queue_num = pdo_count('jy_dish_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'queue_id'=>$queue_id,'status'=>'0','createtime >'=>$customer['createtime']));
		
	   //通知最新队列消息
	   $postdata = array(
			'first' => array(
						'value' => '尊敬的用户您好，您的最新排号信息如下',
						'color' => '#990000'
					),
					'keyword1' => array(
						'value' => $queue_id,
						'color' => '#ff510'
					),
					'keyword2' => array(
						'value' => date("Y-m-d H:i:s",$customer['createtime']),
						'color' => '#ff510'
					),
					'keyword3' => array(
						'value' => $queue_num,
						'color' => '#ff510'
					),
					'remark' => array(
						'value' =>'感谢您的支持！',
						'color' => '#ff510'
					),							
		);
		$tpl=$this->plugin_setting('message_tpl');
		$url=custom_url('jy_dish/queue_manage/',true);
		cfc('account')->sendTplNotice($c,$tpl['queue_update'],$postdata,$url,'#980000');
	}
}
	exi('叫号成功');
}
